<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="信息来源" prop="infoFrom">
        <el-select
          v-model="queryParams.infoFrom"
          placeholder="请选择信息来源"
          clearable
        >
          <el-option label="平台" :value="1" />
          <el-option label="中国算力网" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="信息标题" prop="infoTitle">
        <el-input
          v-model="queryParams.infoTitle"
          placeholder="请输入信息标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信息大类" prop="infoType">
        <el-select
          v-model="queryParams.infoType"
          placeholder="请选择信息大类"
          clearable
        >
          <el-option
            v-for="dict in dict.type.info_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['info:news:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['info:news:edit']"
          >修改</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['system:base:remove']"-->
      <!--          >删除</el-button-->
      <!--        >-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['system:base:export']"-->
      <!--          >导出</el-button-->
      <!--        >-->
      <!-- </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="baseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="信息大类" align="center" prop="infoType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.info_type"
            :value="scope.row.infoType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="信息标题"
        align="center"
        prop="infoTitle"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column label="信息来源" align="center" prop="infoFrom">
        <template #default="{ row }">{{
          row.infoFrom === 1 ? "平台" : "中国算力网"
        }}</template>
      </el-table-column>
      <el-table-column label="跳转类型" align="center">
        <template #default="{ row }">{{
          row.openType === 1
            ? "跳转链接"
            : row.openType === 2
            ? "跳转详情"
            : "不跳转"
        }}</template>
      </el-table-column>
      <el-table-column
        label="通知订阅人"
        align="center"
        prop="noticeUserStatus"
      >
        <template #default="{ row }">{{
          row.noticeUserStatus ? "通知" : "不通知"
        }}</template>
      </el-table-column>
      <el-table-column
        label="信息简单描述"
        align="center"
        prop="infoSimpleDesc"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        label="信息大图"
        align="center"
        prop="infoPicUrl"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            v-if="scope.row.infoPicUrl"
            :src="scope.row.infoPicUrl"
            :width="50"
            :height="50"
          />
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column
        label="跳转链接"
        align="center"
        prop="openUrl"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        label="附件"
        align="center"
        width="180"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-link
            :href="row.attachFileUrl"
            :underline="false"
            target="_blank"
            v-if="row.attachFileUrl"
          >
            <span class="el-icon-document"> {{ row.attachFileName }} </span>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column
        label="信息详细描述"
        align="center"
        prop="infoDetailDesc"
      >
        <template #default="{ row }">
          <el-button type="text" @click="checkNewsDetail(row)"
            >点击查看</el-button
          >
        </template>
      </el-table-column> -->

      <el-table-column label="状态" align="center" prop="status" width="120px">
        <template #default="{ row }">
          <span v-if="row.status === -1">未审核</span>
          <span v-else-if="row.status === -2">已驳回</span>
          <template v-else>
            <el-switch
              :value="row.status"
              :active-value="1"
              :inactive-value="0"
              @change="toggleStatus(row)"
            ></el-switch>
            <span>{{ row.status ? "上架" : "下架" }}</span>
          </template>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['info:news:edit']"
            >修改</el-button
          >
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['info:news:remove']"-->
          <!--            >删除</el-button-->
          <!--          >-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基本信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="40vw"
      :close-on-click-modal="false"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="信息大类" prop="infoType">
          <el-select v-model="form.infoType" placeholder="请选择信息大类">
            <el-option
              v-for="dict in dict.type.info_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="信息标题" prop="infoTitle">
          <el-input v-model="form.infoTitle" placeholder="请输入信息标题" />
        </el-form-item>
        <el-form-item label="信息来源" prop="infoFrom">
          <el-select v-model="form.infoFrom" placeholder="请选择信息来源">
            <el-option label="平台" :value="1" />
            <el-option label="中国算力网" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="跳转类型" prop="openType">
          <el-select v-model="form.openType" placeholder="请选择跳转类型">
            <el-option label="跳转链接" :value="1" />
            <el-option label="跳转详情" :value="2" />
            <el-option label="不跳转" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否通知订阅人" prop="noticeUserStatus">
          <el-radio-group v-model="form.noticeUserStatus">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="信息简单描述" prop="infoSimpleDesc">
          <el-input
            type="textarea"
            :maxlength="200"
            :autosize="{ minRows: 3 }"
            v-model="form.infoSimpleDesc"
            placeholder="请输入信息简单描述"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="信息大图" prop="infoPicUrl">
          <minio-image-upload
            v-model="form.infoPicUrl"
            :limit="1"
            :fileSize="50"
            accept="image/*,video/*"
            :fileType="['png', 'jpg', 'jpeg', 'mp4', 'avi']"
            :extraTips="form.infoType === 1 ? '轮播图尺寸： 1920 * 620' : ''"
          />
        </el-form-item>
        <el-form-item label="附件" prop="attachFileUrl">
          <fileUpload
            v-model="form.attachFileUrl"
            :limit="1"
            :fileSize="50"
            :fileType="false"
            :nameList="[form.attachFileName]"
            @uploadSuccess="handleFileSuccess"
          />
        </el-form-item>
        <el-form-item label="跳转链接" prop="openUrl">
          <el-input v-model="form.openUrl" placeholder="请输入跳转链接" />
        </el-form-item>
        <el-form-item label="信息详细描述">
          <editor v-model="form.infoDetailDesc" :min-height="252" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBase,
  getBase,
  delBase,
  addBase,
  updateBase,
  toggleNews,
} from "@/api/info/news";

import FileUpload from "@/components/FileUpload/shardUpload.vue";

export default {
  name: "Base",
  dicts: ["info_type"],
  components: { FileUpload },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基本信息表格数据
      baseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoDetailDesc: null,
        infoFrom: 1,
        infoTitle: null,
        infoType: null,
        noticeUserStatus: 0,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // infoDetailDesc: [
        //   { required: true, message: "信息详细描述不能为空", trigger: "blur" },
        // ],
        infoFrom: [
          {
            required: true,
            message: "信息来源不能为空",
            trigger: "blur",
          },
        ],
        // infoPicUrl: [
        //   { required: true, message: "信息大图不能为空", trigger: "blur" },
        // ],
        // infoSimpleDesc: [
        //   { required: true, message: "信息简单描述不能为空", trigger: "blur" },
        // ],
        // infoTitle: [
        //   { required: true, message: "信息标题不能为空", trigger: "blur" },
        // ],
        infoType: [
          {
            required: true,
            message: "信息类型不能为空",
            trigger: "change",
          },
        ],
        noticeUserStatus: [
          {
            required: true,
            message: "是否通知订阅人不能为空",
            trigger: "change",
          },
        ],
      },
      currRow: {},
      detailVisible: false,
      isAdd: false,
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    window.addEventListener("beforeunload", this.unloadFunc);
  },
  beforeDestroy() {
    window.removeEventListener("beforeunload", this.unloadFunc);
  },
  methods: {
    unloadFunc() {
      if (this.open && this.isAdd) {
        localStorage.setItem("news_add", JSON.stringify(this.form));
      }
    },
    handleFileSuccess({ name }) {
      this.form.attachFileName = name;
    },
    async toggleStatus(row) {
      const { id, status } = row;
      const { code } = await toggleNews({
        id,
        status: Number(!status),
      });
      if (code === 200) {
        this.$message.success(status ? "下架成功" : "上架成功");
        this.$set(row, "status", Number(!status));
      }
    },
    checkNewsDetail(row) {
      this.currRow = row;
      this.detailVisible = true;
    },
    /** 查询基本信息列表 */
    getList() {
      this.loading = true;
      listBase(this.queryParams).then((response) => {
        this.baseList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        createBy: null,
        createTime: null,
        createUserId: null,
        isDelete: null,
        remark: null,
        updateBy: null,
        updateTime: null,
        updateUserId: null,
        infoDetailDesc: null,
        infoFrom: 1,
        infoPicUrl: null,
        infoSimpleDesc: null,
        infoTitle: null,
        infoType: null,
        nodeId: null,
        noticeUserStatus: 0,
        openUrl: null,
        dictType: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isAdd = true;
      this.setStorageAdd();
      this.open = true;
      this.title = "添加基本信息";
    },
    setStorageAdd() {
      const addNewsJson = localStorage.getItem("news_add");
      if (!addNewsJson) return;
      this.form = JSON.parse(addNewsJson);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isAdd = false;
      const id = row.id || this.ids;
      getBase(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBase(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBase(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              localStorage.removeItem("news_add");
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除基本信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBase(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/base/export",
        {
          ...this.queryParams,
        },
        `base_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
